//
//  YYScanView.m
//  Project
//
//  Created by Yown on 2024/1/25.
//

#import "YYScanView.h"
#import "YYScanCell.h"

#import <BIBluetoothModule/BIBluetoothManager.h>
#import <BIProjectModule/BITableView.h>
#import <BIToolModule/BIProjectDefine.h>

#import <BIToolModule/NSArray+Helper.h>
#import <BIProjectModule/BIAppDefine.h>
#import <MBProgressHUD/MBProgressHUD.h>

@interface YYScanView () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;
@property (weak, nonatomic) IBOutlet UILabel *searchLabel;

@property (weak, nonatomic) IBOutlet BITableView *tableView;

@end

@implementation YYScanView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.backgroundColor = [UIColor loadColorFromString:@"#F6F6F6"];
    
    NSMutableArray *imageArray = [NSMutableArray array];
    for (NSUInteger i = 0; i < 30; i++) {
        NSString *imageName = [NSString stringWithFormat:@"scan_bg%ld", i];
        UIImage *image = [UIImage imageNamed:imageName];
        [imageArray addObject:image];
    }
    self.bgImageView.animationImages = [imageArray copy];
    [self.bgImageView startAnimating];
    
    self.searchLabel.text = BILocalizedString(@"scan_tip");
    self.searchLabel.textColor = [UIColor loadColorFromString:@"#828282"];
    self.searchLabel.font = BIFont(14);
    
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"YYScanCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:[YYScanCell cellIdentifier]];
}

- (void)reloadData {
    [self.tableView reloadData];
}

#pragma mark - UITableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [BIBluetoothManager sharedInstance].allPeripherals.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [YYScanCell cellHeight];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BIBluetoothModel *currentModel = [BIBluetoothManager sharedInstance].currentBluetoothModel;

    BIBluetoothModel *model = [[BIBluetoothManager sharedInstance].allPeripherals safeObjectAtIndex:indexPath.row];
    YYScanCell *cell = [tableView dequeueReusableCellWithIdentifier:[YYScanCell cellIdentifier] forIndexPath:indexPath];
    @weakify(self);
    cell.connectBlock = ^{
        @strongify(self);
        // 先断开当前蓝牙
        [[BIBluetoothManager sharedInstance] disconnectBluetooth:currentModel];
        [[BIBluetoothManager sharedInstance] connectBluetooth:model];
        BISafeBlock(self.connectBlock);
    };
    if ([currentModel.uuid isEqualToString:model.uuid]) {
        [cell reloadDeivceName:model.name image:model.deviceImageURL uuid:model.macAddress buttonText:@""];
    } else {
        [cell reloadDeivceName:model.name image:model.deviceImageURL uuid:model.macAddress buttonText:@"scan_connect"];
    }
    return cell;
}

@end
